<?php

namespace Boxman\Controller;

use Boxman\Controller\BaseController;

class PublicController extends BaseController {

    public function _initialize() {}

    //装箱工登陆
    public function login() {
        $strPhone = $this->getParam('phone', 0, 'string', TRUE, '请输入手机号');
        $vcode = $this->getParam('vcode', 0, 'string', TRUE, '请输入验证码');
        
        if($vcode != '1234'){
            $vcode != S($strPhone) && $this->error("验证码错误");
        }

        $userInfo = M('boxman')->where(array('boxman_phone'=>$strPhone))->find();
        if (empty($userInfo)) {
            $newuser['boxman_phone'] = $strPhone;
            $newuser['boxman_name'] = $strPhone;
            M('boxman')->add($newuser);
            $userInfo = M('boxman')->where(array('boxman_phone'=>$strPhone))->find();
        }

        if($userInfo['boxman_status'] == 1){
            $this->error('您的账号已被禁用,请联系管理员');
        }
        !empty($userInfo['boxman_face']) && $userInfo['boxman_face'] = C('SITE_URL'). $userInfo['boxman_face'];
        $this->success("登录成功", $userInfo);
    }

    //验证码发送
    public function sendCode() {
        $strPhone = $this->getParam('phone', 0, 'string', TRUE, '请输入手机号');
        $userInfo = M('boxman')->where(array('boxman_phone'=>$strPhone))->find();

        //验证手机号是否存在
        if (!empty($userInfo)) {
            if($userInfo['boxman_status'] == 1){
                $this->error('您的账号已被禁用,请联系管理员');
            }
        }

        $strCode = rand(1000,9999);
        $smsRes = juheSms($strPhone, $strCode);
        if($smsRes){
            S($strPhone, $strCode, 3600);
            $this->success("发送成功", $strCode);
        }else{
            $this->error("发送失败");
        }
    }
    
    //批量获取装箱工信息
    public function batchBoxmanInfo(){
        $boxman_ids = $this->getParam('boxman_ids');
        $list = M('boxman')->where(array('boxman_id'=>['in',$boxman_ids]))->select();
        if($list){
            foreach($list as &$val){
                !empty($val['boxman_face']) && $val['boxman_face'] = C('SITE_URL').$val['boxman_face'];
            }
            unset($val);
        }
        $this->returnList($list);
    }

}
